package com.community.util;

import com.community.common.Const;
import com.community.pojo.User;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录操作公共类
 */
public class LoginUtil {

    /**
     * 判断是否登录，返回User对象
     * @param request
     * @return
     */
    public static User isLogin(HttpServletRequest request){
        User user = null;
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null != o){
            user = (User)o;
        }
        return user;
    }


    /**
     * 判断参数用户名与登录用户名是否一致
     * @param request
     * @param userName
     * @return
     */
    public static boolean isLogin(HttpServletRequest request,String userName){
        boolean flag = false;
        User user = null;
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null != o){
            user = (User)o;
        }
        if(user != null){
            if(StringUtil.valueOfString(user.getUsername()).equals(userName)){
                flag = true;
            }
        }
        return flag;
    }

}
